package com.nop.mdp.services;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nop.mdp.beans.MdpUsers;

/**
 * 
 * @author gtourvieille
 * @created 15 avr. 08
 * @date $Date: 2008-07-03 18:13:15 +0200 (jeu., 03 juil. 2008) $
 * @version $Id: UserService.java 7697 2008-07-03 16:13:15Z gtourvieille $
 */

public class MdpService extends BaseService
{
	
	private static final Logger	LOG	= LoggerFactory.getLogger( MdpService.class );
	
	/**
	 * Get a MdpUser giving by his login
	 * 
	 * @param mdp_user_login
	 * @return MdpUser
	 */
	public MdpUsers getMdpUser( String mdp_user_login )
	{
		final Session ses = this.getSession();
		final Query q = ses.createQuery( "from MdpUsers where mdp_user_login = ?" );
		q.setString( 0, mdp_user_login );
		
		final Object aResult = q.uniqueResult();
		
		if ( aResult instanceof MdpUsers )
		{
			final MdpUsers mdpUser = (MdpUsers)aResult;
			return mdpUser;
		}
		return null;
	}
	
	/**
	 * Create a MdpUser
	 *  
	 * @param mdp_user_login
	 * @param mdp_user_pass
	 * @return mdpUser
	 */
	public MdpUsers addMdpUser( String mdp_user_login, String mdp_user_pass )
	{
		MdpService.LOG.debug( "creating mdpUser : " + mdp_user_login );
		MdpUsers mdpUser = new MdpUsers();
		mdpUser.setMdp_user_login( mdp_user_login );
		mdpUser.setMdp_user_pass( mdp_user_pass );
		
		final Session ses = this.getSession();
		ses.save( mdpUser );
		MdpService.LOG.debug( "create mdpUser with ID : " + mdpUser.getMdp_user_id() );
		return mdpUser;
	}
	
}
